package models

import (
	"go-admin/global/orm"
	"go-admin/tools"
	"time"
	_ "time"
)

type ZxDesign struct {
	Id          int       `json:"id" gorm:"type:int(11) unsigned;primary_key"`        //
	DesignerId  int       `json:"designerId,omitempty" gorm:"type:int(11) unsigned;"` // 设计者id
	DesignPrice int       `json:"designPrice,omitempty" gorm:"type:int(10);"`         // 设计报价
	DesignState string    `json:"designState,omitempty"`
	DesignImgs  string    `json:"designImgs" gorm:"type:varchar(200);"`        // 设计图
	FinishDate  time.Time `json:"finishDate,omitempty" gorm:"type:timestamp;"` // 完成日期
	Note        string    `json:"note" gorm:"type:varchar(200);"`              // 备注
	CreateBy    string    `json:"createBy" gorm:"type:varchar(128);"`          // 创建者
	UpdateBy    string    `json:"updateBy" gorm:"type:varchar(128);"`          // 更新者
	DataScope   string    `json:"dataScope" gorm:"-"`
	Params      string    `json:"params"  gorm:"-"`
	BaseModel
}

func (ZxDesign) TableName() string {
	return "zx_design"
}

// 创建ZxDesign
func (e *ZxDesign) Create() (ZxDesign, error) {
	var doc ZxDesign
	result := orm.Eloquent.Table(e.TableName()).Create(&e)
	if result.Error != nil {
		err := result.Error
		return doc, err
	}
	doc = *e
	return doc, nil
}

// 获取ZxDesign
func (e *ZxDesign) Get() (ZxDesign, error) {
	var doc ZxDesign
	table := orm.Eloquent.Table(e.TableName())

	if e.Id != 0 {
		table = table.Where("id = ?", e.Id)
	}

	//if e.Designer != ""{
	//	table = table.Where("designer = ?", e.Designer)
	//}

	if err := table.First(&doc).Error; err != nil {
		return doc, err
	}
	return doc, nil
}

// 获取ZxDesign带分页
func (e *ZxDesign) GetPage(pageSize int, pageIndex int) ([]ZxDesign, int, error) {
	var doc []ZxDesign

	table := orm.Eloquent.Select("*").Table(e.TableName())

	// 数据权限控制(如果不需要数据权限请将此处去掉)
	dataPermission := new(DataPermission)
	dataPermission.UserId, _ = tools.StringToInt(e.DataScope)
	table, err := dataPermission.GetDataScope(e.TableName(), table)
	if err != nil {
		return nil, 0, err
	}
	var count int

	if err := table.Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&doc).Error; err != nil {
		return nil, 0, err
	}
	table.Where("`deleted_at` IS NULL").Count(&count)
	return doc, count, nil
}

// 更新ZxDesign
func (e *ZxDesign) Update() (update ZxDesign, err error) {
	if err = orm.Eloquent.Table(e.TableName()).Where("id = ?", e.Id).First(&update).Error; err != nil {
		return
	}

	//参数1:是要修改的数据
	//参数2:是修改的数据
	if err = orm.Eloquent.Table(e.TableName()).Model(&update).Updates(&e).Error; err != nil {
		return
	}
	return
}

// 删除ZxDesign
func (e *ZxDesign) Delete(id int) (success bool, err error) {
	if err = orm.Eloquent.Table(e.TableName()).Where("id = ?", id).Delete(&ZxDesign{}).Error; err != nil {
		success = false
		return
	}
	success = true
	return
}

//批量删除
func (e *ZxDesign) BatchDelete(id []int) (Result bool, err error) {
	if err = orm.Eloquent.Table(e.TableName()).Where("id in (?)", id).Delete(&ZxDesign{}).Error; err != nil {
		return
	}
	Result = true
	return
}
